package com.kangyi.user.controller;

import com.kangyi.dto.ApplicationListDTO;
import com.kangyi.dto.NursingHomeApplicationDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.user.service.NursingHomeApplicationService;
import com.kangyi.vo.NursingHomeApplicationDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/user/application")
@Api(tags = "养老院入驻相关接口")
@Slf4j
public class NursingHomeApplicationController {
    @Autowired
    private NursingHomeApplicationService applicationService;
    @PostMapping("/submit")
    @ApiOperation("养老院入驻申请")
    @CacheEvict(cacheNames = "application_", allEntries = true)
    public Result nursingHomeApplication(@RequestBody NursingHomeApplicationDTO nursingHomeApplicationDTO){
        log.info("养老院入驻申请:{}",nursingHomeApplicationDTO);
        try {
            applicationService.submit(nursingHomeApplicationDTO);
            return Result.success("提交成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/list")
    @ApiOperation("查询申请记录")
    public Result<PageResult> listApplication(ApplicationListDTO applicationListDTO){
        log.info("查询申请记录:{}",applicationListDTO);
        try {
            PageResult pageResult = applicationService.listApplication(applicationListDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{applicationId}")
    @ApiOperation("撤回申请")
    @CacheEvict(cacheNames = "application_", allEntries = true)
    public Result deleteApplication(@PathVariable Integer applicationId){
        log.info("撤回申请:{}",applicationId);
        try {
            applicationService.deleteApplication(applicationId);
            return Result.success("撤回成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/detail/{applicationId}")
    @ApiOperation("查询申请详情")
    @Cacheable(cacheNames = "application_", key = "'userDetail_'+#applicationId")
    public Result<NursingHomeApplicationDetailVO> DetailApplication(@PathVariable Integer applicationId){
        log.info("查询申请详情:{}",applicationId);
        try {
            NursingHomeApplicationDetailVO nursingHomeApplicationDetailVO = applicationService.DetailApplication(applicationId);
            return Result.success(nursingHomeApplicationDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
